import type { PageParams, PageResult } from '@/types/global'
import type { BannerItem, CategoryItem, GuessItem, HotItem } from '@/types/home'
import type { HotResult } from '@/types/hot'
import type { CategoryTopItem } from '@/types/kind'
import type { GoodsResult } from '@/types/goods'
import type { CartItem } from '@/types/cart'
import { http } from '@/utils/http'
import type { LoginResult, ProfileDetail, ProfileParams } from '@/types/member'
import type { AddressParams,AddressItem } from '@/types/address'

/**
 * 首页-广告区域-小程序
 * @param distributionSite 广告区域展示位置（投放位置 投放位置，1为首页，2为分类商品页） 默认是1
 */

export const getHomeBannerAPI = (distributionSite = 1) => {
  return http<BannerItem[]>({
    method: 'GET',
    url: '/home/banner',
    data: {
      distributionSite,
    },
  })
}

/**
 * 首页-前台分类-小程序
 */
export const getHomeCategoryAPI = () => {
  return http<CategoryItem[]>({
    method: 'GET',
    url: '/home/category/mutli',
  })
}

/**
 * 首页-热门推荐-小程序
 */
export const getHomeHotAPI = () => {
  return http<HotItem[]>({
    method: 'GET',
    url: '/home/hot/mutli',
  })
}

/**
 * 猜你喜欢-小程序
 */
export const getHomeGoodsGuessLikeAPI = (data?: PageParams) => {
  return http<PageResult<GuessItem>>({
    method: 'GET',
    url: '/home/goods/guessLike',
    data,
  })
}
/**
 * 通用热门推荐类型
 * @param url 请求地址
 * @param data 请求参数
 */
// 定义新传惨类型
type HotParams = PageParams & { subType?:string }
export const getHotRecommendAPI = (url:string,data?:HotParams) => {
  return http<HotResult>({
    method:'GET',
    url,
    data,
  })
}

/**
 * 分类列表-小程序
 */
export const getCategoryTopAPI = () => {
  return http<CategoryTopItem[]>({
    method: 'GET',
    url: '/category/top',
  })
}

/**
 * 商品详情
 * @param id 商品id
 */
export const getGoodsByIdAPI = (id: string) => {
  return http<GoodsResult>({
    method: 'GET',
    url: '/goods',
    data: { id },
  })
}
/**
 * 加入购物车
 * @param data 请求体参数
 */
export const postMemberCartAPI = (data: { skuId: string; count: number }) => {
  return http({
    method: 'POST',
    url: '/member/cart',
    data,
  })
}

/**
 * 获取购物车列表
 */
export const getMemberCartAPI = () => {
  return http<CartItem[]>({
    method: 'GET',
    url: '/member/cart',
  })
}

/**
 * 删除/清空购物车单品
 * @param data 请求体参数 ids SKUID 集合
 */
export const deleteMemberCartAPI = (data: { ids: string[] }) => {
  return http({
    method: 'DELETE',
    url: '/member/cart',
    data,
  })
}

/**
 * 修改购物车单品
 * @param skuId SKUID
 * @param data selected 选中状态 count 商品数量
 */
export const putMemberCartBySkuIdAPI = (
  skuId: string,
  data: { selected?: boolean; count?: number },
) => {
  return http({
    method: 'PUT',
    url: `/member/cart/${skuId}`,
    data,
  })
}

/**
 * 购物车全选/取消全选
 * @param data selected 是否选中
 */
export const putMemberCartSelectedAPI = (data: { selected: boolean }) => {
  return http({
    method: 'PUT',
    url: '/member/cart/selected',
    data,
  })
}

type LoginWxMinParams = {
  code:string
  encryptedData?:string
  iv?:string
}
/** 小程序登陆
 * @param data 请求参数
 */
export const postLoginWxMinAPI = (data: LoginWxMinParams) => {
  return http<LoginResult>({
    method:'POST',
    url:'/login/wxMin',
    data,
  })
}
/**
 * 小程序登录_内测版
 * @param phoneNumber 模拟手机号码
 */
export const postLoginWxMinSimpleAPI = (phoneNumber: string) => {
  return http<LoginResult>({
    method: 'POST',
    url: '/login/wxMin/simple',
    data: {
      phoneNumber,
    },
  })
}

type LoginParams = {
  account: string
  password: string
}
/**
 * 传统登录-用户名+密码
 * @param data 请求参数
 */
export const postLoginAPI = (data: LoginParams) => {
  return http<LoginResult>({
    method: 'POST',
    url: '/login',
    data,
  })
}
/** 获取个人信息 */
export const getMemberProfileAPI = () => {
  return http<ProfileDetail>({
    method:'GET',
    url:'/member/profile'
  })
}
/** 修改个人信息 */
export const putMemberProfileAPI = (data: ProfileParams) => {
    return http<ProfileParams>({
      method:'PUT',
      url:'/member/profile',
      data,
    })
}
/**
 * 添加收货地址
 */
export const postMemberAddressAPI = (data: AddressParams) => {
  return http({
    method:'POST',
    url:'/member/address',
    data,
  })
}
/**
 * 获取收货地址列表
 */
export const getMemberAddressAPI = () => {
  return http<AddressItem[]>({
    method:'GET',
    url:'/member/address',
  })
}
/**
 * 获取收货地址详情
 */
export const getMemberAddressByIdAPI = (id: string) => {
  return http<AddressItem>({
    method:'GET',
    url:`/member/address/${id}`,
  })
}
/**
 * 修改收货地址
 */
export const putMemberAddressByIdAPI = (id: string,data: AddressParams) => {
  return http<AddressItem>({
    method:'PUT',
    url:`/member/address/${id}`,
    data,
  })
}
/**
 * 删除收货地址
 */
export const deleteMemberAddressByIdAPI = (id: string) => {
  return http({
    method:'DELETE',
    url:`/member/address/${id}`,
  })
}